package net.xdclass.strategy;


import net.xdclass.enums.BizCodeEnum;
import net.xdclass.exception.BizException;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingValue;

import java.util.Collection;

public class CustomDBPreciseShardingAlgorithm implements PreciseShardingAlgorithm<String> {

    @Override
    public String doSharding(Collection<String> collection, PreciseShardingValue<String> preciseShardingValue) {
        //获取短链码第一位，库位
        String codePrefix = preciseShardingValue.getValue().substring(0, 1);

        for (String targetName : collection) {
            // 库名的最后一位
            String suffix = targetName.substring(targetName.length() - 1);
            if (codePrefix.equals(suffix)) {
                return targetName;
            }
        }
        throw new BizException(BizCodeEnum.DB_ROUTE_NOT_FOUND);
    }
}
